package com.example.highperformanceandroidtech.fragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.highperformanceandroidtech.R;

public class Example1_3Fragment extends Fragment {
    private ImageView imageView;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_example1_3,container,false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        imageView = view.findViewById(R.id.imageView);
        Button btnStart = view.findViewById(R.id.btnStart);
        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnim();
            }
        });
    }

    private void startAnim(){
        //定义一个过度Drawable数组，最后一个Drawable:drawables[n - 1]是最终的显示形式
        //第一个drawable[0]是开始
        Drawable[] drawables = new Drawable[]{
                new ColorDrawable(Color.TRANSPARENT),
                new ColorDrawable(Color.BLUE),
                new ColorDrawable(Color.GREEN),
                new ColorDrawable(Color.RED)
        };
        TransitionDrawable transitionDrawable = new TransitionDrawable(drawables);
        imageView.setImageDrawable(transitionDrawable);
        //启用交叉淡入样式
        transitionDrawable.setCrossFadeEnabled(true);
        //开始执行动画
        transitionDrawable.startTransition(15000);
    }
}
